import os
from bs4 import BeautifulSoup
import shutil

def convert_layui_to_vue(html_file):
    with open(html_file, 'r', encoding='utf-8') as f:
        soup = BeautifulSoup(f, 'html.parser')
    
    # 转换表格组件
    for table in soup.select('table.layui-table'):
        new_table = f'<a-table :dataSource="{table.get("id")}Data" />'
        table.replace_with(BeautifulSoup(new_table, 'html.parser'))
    
    # 转换表单
    for form in soup.select('form.layui-form'):
        inputs = []
        for inp in form.select('input'):
            input_type = inp.get('type', 'text')
            if input_type == 'text':
                inputs.append(f'<a-input name="{inp.get("name")}" />')
            elif input_type == 'checkbox':
                inputs.append(f'<a-checkbox name="{inp.get("name")}">{inp.next_sibling}</a-checkbox>')
        new_form = f'<a-form>\n{"".join(inputs)}\n</a-form>'
        form.replace_with(BeautifulSoup(new_form, 'html.parser'))
    
    # 保存为Vue文件
    vue_file = html_file.replace('.html', '.vue').replace('layui_legacy', 'vue-app/src/views')
    os.makedirs(os.path.dirname(vue_file), exist_ok=True)
    with open(vue_file, 'w', encoding='utf-8') as vf:
        vf.write(f'<template>\n{soup.prettify()}\n</template>')

# 主程序
project_root = r'D:\phpstudy_pro\WWW\oa.laizone.com'
legacy_dir = os.path.join(project_root, 'migration', 'frontend', 'layui_legacy')

for root, dirs, files in os.walk(legacy_dir):
    for file in files:
        if file.endswith('.html'):
            convert_layui_to_vue(os.path.join(root, file))

print(" Layui组件转换完成")